package com.chensi.common;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import com.chensi.util.PropertiesLoader;
import com.google.common.collect.Maps;
import com.google.gson.Gson;

/**
 * 全局配置类
 * @author Chason
 * @version 2016-1-15 上午11:10:04
 */
public class Global {
	/** 当前对象实例 */
	private static Global global = new Global();

	/** 保存全局属性值 */
	private static Map<String, String> map = Maps.newHashMap();
	
	/** json转换常量 */
	public static final Gson gson = new Gson();

	/** 属性文件加载对象 */
	private static PropertiesLoader propertiesLoader = new PropertiesLoader("config.properties");

	/** 获取当前对象实例 */
	public static Global getInstance() {
		return global;
	}

	/** 获取配置 */
	public static String getConfig(String key) {
		String value = map.get(key);
		if (value == null) {
			value = propertiesLoader.getProperty(key);
			map.put(key, value != null ? value : "");
		}
		try {
			return new String(value.getBytes("iso8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "";
	}

	/** 缓存是否开启 */
	public static Boolean isCacheMode() {
		String dm = getConfig("sys.cacheEnable");
		return "true".equals(dm) || "1".equals(dm);
	}
}
